package com.fx.ruankao.service.impl;

import com.fx.ruankao.entity.QuestionBank;
import com.fx.ruankao.mapper.QuestionBankMapper;
import com.fx.ruankao.mapper.QuestionOptionMapper;
import com.fx.ruankao.service.QuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 题库服务实现类
 */
@Service
@Transactional
public class QuestionBankServiceImpl implements QuestionBankService {
    
    @Autowired
    private QuestionBankMapper questionBankMapper;
    
    @Autowired
    private QuestionOptionMapper questionOptionMapper;
    
    @Override
    public int insert(QuestionBank questionBank) {
        return questionBankMapper.insert(questionBank);
    }
    
    @Override
    public int deleteById(Integer id) {
        return questionBankMapper.deleteById(id != null ? id.longValue() : null);
    }
    
    @Override
    public int update(QuestionBank questionBank) {
        return questionBankMapper.update(questionBank);
    }
    
    @Override
    public QuestionBank selectById(Integer id) {
        return questionBankMapper.selectById(id != null ? id.longValue() : null);
    }
    
    @Override
    public List<QuestionBank> selectAll() {
        return questionBankMapper.selectList(new QuestionBank());
    }
    
    @Override
    public List<QuestionBank> selectByQuestionType(String questionType) {
        QuestionBank questionBank = new QuestionBank();
        // 将String类型转换为Integer类型
        if (questionType != null && !questionType.isEmpty()) {
            try {
                questionBank.setQuestionType(Integer.parseInt(questionType));
            } catch (NumberFormatException e) {
                // 处理转换错误，这里简单忽略
            }
        }
        return questionBankMapper.selectList(questionBank);
    }
    
    @Override
    public List<QuestionBank> selectBySubject(String subject) {
        // QuestionBank实体类中没有subject字段，使用chapter字段代替
        QuestionBank questionBank = new QuestionBank();
        questionBank.setChapter(subject);
        return questionBankMapper.selectList(questionBank);
    }
    
    @Override
    public List<QuestionBank> selectByDifficulty(String difficulty) {
        QuestionBank questionBank = new QuestionBank();
        // 将String类型转换为Integer类型
        if (difficulty != null && !difficulty.isEmpty()) {
            try {
                questionBank.setDifficulty(Integer.parseInt(difficulty));
            } catch (NumberFormatException e) {
                // 处理转换错误，这里简单忽略
            }
        }
        return questionBankMapper.selectList(questionBank);
    }
    
    @Override
    public int batchDelete(Integer[] ids) {
        // QuestionBankMapper接口中没有batchDelete方法，这里通过循环删除实现
        int count = 0;
        if (ids != null) {
            for (Integer id : ids) {
                count += questionBankMapper.deleteById(id != null ? id.longValue() : null);
            }
        }
        return count;
    }
    
    @Transactional
    @Override
    public int batchImport(List<QuestionBank> questionBanks) {
        int count = 0;
        for (QuestionBank questionBank : questionBanks) {
            int result = questionBankMapper.insert(questionBank);
            if (result > 0) {
                count++;
                // 注意：QuestionBank实体类中没有getOptions()方法
                // 如果需要处理题目选项，应该有专门的方式
            }
        }
        return count;
    }
    
    @Override
    public List<QuestionBank> getRandomQuestions(int count, String questionType, String subject, String difficulty) {
        // 将String类型的参数转换为mapper方法需要的Integer类型
        // 这里只是简单的示例转换，实际应该根据业务逻辑处理
        Integer type = null;
        if (questionType != null && !questionType.isEmpty()) {
            try {
                type = Integer.parseInt(questionType);
            } catch (NumberFormatException e) {
                // 处理转换错误，这里简单忽略
            }
        }
        
        Integer diffLevel = null;
        if (difficulty != null && !difficulty.isEmpty()) {
            try {
                diffLevel = Integer.parseInt(difficulty);
            } catch (NumberFormatException e) {
                // 处理转换错误，这里简单忽略
            }
        }
        
        return questionBankMapper.selectRandomQuestions(
            type, 
            diffLevel, 
            subject, 
            null, 
            count
        );
    }
    
    @Override
    public int countQuestions() {
        return questionBankMapper.selectCount(new QuestionBank());
    }
    
    @Override
    public int countQuestionsByCondition(String questionType, String subject, String difficulty) {
        QuestionBank questionBank = new QuestionBank();
        // 将String类型转换为Integer类型
        if (questionType != null && !questionType.isEmpty()) {
            try {
                questionBank.setQuestionType(Integer.parseInt(questionType));
            } catch (NumberFormatException e) {
                // 处理转换错误，这里简单忽略
            }
        }
        // 设置章节而不是科目
        questionBank.setChapter(subject);
        // 将String类型转换为Integer类型
        if (difficulty != null && !difficulty.isEmpty()) {
            try {
                questionBank.setDifficulty(Integer.parseInt(difficulty));
            } catch (NumberFormatException e) {
                // 处理转换错误，这里简单忽略
            }
        }
        return questionBankMapper.selectCount(questionBank);
    }
}